package ysc.cqnu.operdb.serviceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import ysc.cqnu.operdb.entity.ResultAssemble;
import ysc.cqnu.operdb.entity.ResultData;
import ysc.cqnu.operdb.mapper.ZhuangpeiMapper;
import ysc.cqnu.operdb.service.UpdateZhuangPeiInfoService;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class UpdateZhuangPeiInfoServiceImpl implements UpdateZhuangPeiInfoService {
    @Autowired
    private ResultData resultData;
    @Autowired
    private ZhuangpeiMapper zhuangpeiMapper;
    @Override
    public ResultData updateZhuangPeiInfoService(String zhuangpei, String visualize, String purpose, String theSite, MultipartFile Img, MultipartFile Video, String bfDescribe, String id) {
        String fileName = Img.getOriginalFilename();
        String fileName1 = Video.getOriginalFilename();
        //判断是否是.***格式
        int start = fileName.indexOf(".");
        if (start==-1){
            resultData.flag="typeError";
            resultData.data=null;
            return resultData;
        }

        //判断是不是图片
        String type = fileName.substring(start+1);
        if((!"jpg".equals(type))&&(!"png".equals(type))&&(!"bmp".equals(type))&&(!"JPG".equals(type))&&(!"jpeg".equals(type))){
            resultData.flag="typeError";
            resultData.data=null;
            return resultData;
        }

        //判断是否是.***格式
        int start1 = fileName1.indexOf(".");
        if (start1==-1){
            resultData.flag="typeError";
            resultData.data=null;
            return resultData;
        }

        //判断是不是视频
        String type1 = fileName1.substring(start1+1);
        if(!("mp4".equals(type1))){
            resultData.flag="typeError";
            resultData.data=null;
            return resultData;
        }
        //图片存入的绝对路径
        String filePath = "E://up360/apache-tomcat-9.0.37/webapps/drawingImg/";
        File targetFile = new File(filePath);

        //将图片命名为 用户名+时间+.***
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("y-MM-dd-hh-mm-ss");
        String nowdate = dateFormat.format(date);
        fileName = zhuangpei+nowdate+"."+type;

        if(!targetFile.exists()){
            targetFile.mkdirs();
        }

        FileOutputStream out = null;
        try {
            out = new FileOutputStream(filePath+fileName);
            out.write(Img.getBytes());
            out.flush();
            out.close();

        } catch (Exception e) {
            resultData.flag="ZhuangpeiInfoFail";
            resultData.data=null;
            return resultData;
        }
        //视频存入的绝对路径
        String filePath1 = "E://up360/apache-tomcat-9.0.37/webapps/drawingVideo/";
        File targetFile1 = new File(filePath1);

        //将视频命名为 用户名+时间+.***
        fileName1 = zhuangpei+nowdate+"."+type1;

        if(!targetFile1.exists()){
            targetFile1.mkdirs();
        }

        FileOutputStream out1 = null;
        try {
            out1 = new FileOutputStream(filePath1+fileName1);
            out1.write(Video.getBytes());
            out1.flush();
            out1.close();
            String Image1Src="http://192.168.43.161:8080/drawingVideo/"+fileName1;
            String ImageSrc="http://192.168.43.161:8080/drawingImg/"+fileName;
            zhuangpeiMapper.updateZhuangpeiInfo(visualize,purpose,theSite,ImageSrc,Image1Src,bfDescribe,id);
            resultData.flag="ZhuangpeiInfoSuccess";
            resultData.data=null;
        } catch (Exception e) {
            e.printStackTrace();
            resultData.flag="ZhuangpeiInfoFail";
            resultData.data=null;
            return resultData;
        }
        return resultData;
    }
}
